from redis import Redis

class RedisCon:
    def __init__(self):
        self.r = Redis(host='localhost',port=6379)
    
    # 存set
    def insert_set(self,key,val):
        for i in val:
            self.r.sadd(key,i)
    # 取set
    def find_set(self,key):
        val = self.r.smembers(key)
        if val:
            list = []
            for i in val:
                list.append(str(i,encoding='utf8'))
            return list
        else:

            return None

    
   # hash：属性型添加
   
    def insert_hash(self,key,params,value):
        self.r.hset(key,params,value)
    # 取某个属性的值
    def get_value_hash(self,key,parmas):
        return self.r.hget(key,parmas)
    # 取所有属性的值
    def get_all_hash(self,key):
        return self.r.hgetall(key)
    # 删除某个属性
    def del_hash(self,key,params):
        self.r.hdel(key,params)
        
    
    # 字符串存取
    def set_str(self,key,value,time=0):
        if time > 0:
            self.r.setex(key,time,value)
        else:
            self.r.set(key,value)
    
    def get_str(self,key):
        return self.r.get(key)

# r = Redis()

# print(r.find_set('21031'))


# import json
rs = RedisCon()
# rs.insert_set('name',['/asdsd','/asdasdasdasd'])
# print(rs.find_set('name'))

# key = 'interface1'
# list=['/abc','/user']
# rs.insert_set(key,list)

# me = rs.find_set(key)
# print(me)
# for i in mes:
#     if '/abc' == str(i,encoding='utf8'):
# print(rs.find_set(key))
        # print(1)
# if '/abc' in json.loads(mes):
#     print(1)
# else:
#     print(2)

